//
//  STEmoticonToolbar.swift
//  STVVeibo
//
//  Created by leergou on 16/7/29.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

private let tagNum = 1000

// 按钮的枚举值
enum STEmoticonToolbarButtonType: Int{
    // 默认表情按钮
    case Default = 0
    // Emoji
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

// 若是 继承自 UIStackView ,则需要 ios9.0及以上版本支持
class STEmoticonToolbar: UIStackView {
    
    // 定义一个属性,保存当前选中的按钮
    var currentButton: UIButton?
    // 定义一个闭包,当点击底部表情按钮时,让 pageView 滚到相应位置
    var closure:((type: STEmoticonToolbarButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 表情按钮点击事件
    func emoticonButtonClick(sender: UIButton){
        // 修改当前选中按钮
//        print(sender.tag)
        setSelectedButton(sender)
        // 执行闭包
        closure?(type: STEmoticonToolbarButtonType(rawValue: sender.tag - tagNum)!)
    }
    
    ////MARK: 提供一个方法,设置 选中按钮的切换
    func setSelectedButtonWithTag(tag: Int){
        let button = viewWithTag(tag + tagNum) as! UIButton
        setSelectedButton(button)
    }
    
    func setSelectedButton(button: UIButton){
        if button == currentButton {
            return
        }
        currentButton?.selected = false
        button.selected = true
        currentButton = button
    }
    
    //MARK: setupUI
    func setupUI(){
        // 设置排列方向
        axis = .Horizontal
        // 设置填充方式
        distribution = .FillEqually
//        backgroundColor = RandomColor()
        addChildButton("默认", imageName: "left", type: STEmoticonToolbarButtonType.Default)
        addChildButton("Emoji", imageName: "mid", type: STEmoticonToolbarButtonType.Emoji)
        addChildButton("浪小花", imageName: "right", type: STEmoticonToolbarButtonType.Lxh)
    }
    
    //MARK: 添加按钮
    func addChildButton(title: String, imageName: String, type: STEmoticonToolbarButtonType){
        let button = UIButton(title: title, titleColro: UIColor.darkGrayColor(), font: 15, target: self, action: "emoticonButtonClick:")
        
        if type == .Default {
            setSelectedButton(button)
        }
        
        button.tag = type.rawValue + tagNum
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        addArrangedSubview(button)
    }
}


//MARK: 继承自 UIView 的方法
extension STEmoticonToolbar{
    
    
//    class STEmoticonToolbar: UIView {
//        
//        override init(frame: CGRect) {
//            super.init(frame: frame)
//            setupUI()
//        }
//        
//        required init?(coder aDecoder: NSCoder) {
//            fatalError("init(coder:) has not been implemented")
//        }
//        
//        //MARK: 表情按钮点击事件
//        func emoticonButtonClick(sender: UIButton){
//            print(sender.tag)
//        }
//        
//        //MARK: setupUI
//        func setupUI(){
//            
//            backgroundColor = RandomColor()
//            let defaultButton = addChildButton("默认", imageName: "left", type: STEmoticonToolbarButtonType.Default)
//            let emojiButton = addChildButton("Emoji", imageName: "mid", type: STEmoticonToolbarButtonType.Emoji)
//            let lxhButton = addChildButton("浪小花", imageName: "right", type: STEmoticonToolbarButtonType.Lxh)
//            
//            // 设置约束
//            defaultButton.snp_makeConstraints { (make) -> Void in
//                make.top.leading.bottom.equalTo(self)
//                make.width.equalTo(emojiButton)
//            }
//            emojiButton.snp_makeConstraints { (make) -> Void in
//                make.top.bottom.equalTo(self)
//                make.leading.equalTo(defaultButton.snp_trailing)
//                make.width.equalTo(lxhButton)
//            }
//            lxhButton.snp_makeConstraints { (make) -> Void in
//                make.top.bottom.equalTo(self)
//                make.leading.equalTo(emojiButton.snp_trailing)
//                make.trailing.equalTo(self)
//            }
//        }
//        
//        
//        //MARK: 添加按钮
//        func addChildButton(title: String, imageName: String, type: STEmoticonToolbarButtonType) -> UIButton{
//            let button = UIButton(title: title, titleColro: UIColor.darkGrayColor(), font: 15, target: self, action: "emoticonButtonClick:")
//            button.tag = type.rawValue
//            button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
//            button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
//            addSubview(button)
//            return button
//        }
//        
//    }

    
}
